home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / ruby / 1.8 / timeout.rb < prev    next >
Encoding:
Ruby Source  |  2010-06-08  |  3.0 KB  |  131 lines

  1. #--
  2. # = timeout.rb
  3. #
  4. # execution timeout
  5. #
  6. # = Copyright
  7. #
  8. # Copyright:: (C) 2000  Network Applied Communication Laboratory, Inc.
  9. # Copyright:: (C) 2000  Information-technology Promotion Agency, Japan
  10. #
  11. #++
  12. #
  13. # = Description
  14. #
  15. # A way of performing a potentially long-running operation in a thread, and
  16. # terminating it's execution if it hasn't finished within fixed amount of
  17. # time.
  18. #
  19. # Previous versions of timeout didn't use a module for namespace. This version
  20. # provides both Timeout.timeout, and a backwards-compatible #timeout.
  21. #
  22. # = Synopsis
  23. #
  24. #   require 'timeout'
  25. #   status = Timeout::timeout(5) {
  26. #     # Something that should be interrupted if it takes too much time...
  27. #   }
  28. #
  29.  
  30. module Timeout
  31.  
  32.   ##
  33.   # Raised by Timeout#timeout when the block times out.
  34.  
  35.   class Error < Interrupt
  36.   end
  37.   class ExitException < ::Exception # :nodoc:
  38.   end
  39.  
  40.   THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
  41.   CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
  42.  
  43.   ##
  44.   # Executes the method's block. If the block execution terminates before +sec+
  45.   # seconds has passed, it returns true. If not, it terminates the execution
  46.   # and raises +exception+ (which defaults to Timeout::Error).
  47.   #
  48.   # Note that this is both a method of module Timeout, so you can 'include
  49.   # Timeout' into your classes so they have a #timeout method, as well as a
  50.   # module method, so you can call it directly as Timeout.timeout().
  51.  
  52.   def timeout(sec, klass = nil)
  53.     return yield if sec == nil or sec.zero?
  54.     raise ThreadError, "timeout within critical session" if Thread.critical
  55.     exception = klass || Class.new(ExitException)
  56.     begin
  57.       x = Thread.current
  58.       y = Thread.start {
  59.         begin
  60.           sleep sec
  61.         rescue => e
  62.           x.raise e
  63.         else
  64.           x.raise exception, "execution expired" if x.alive?
  65.         end
  66.       }
  67.       yield sec
  68.       #    return true
  69.     rescue exception => e
  70.       rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
  71.       (bt = e.backtrace).reject! {|m| rej =~ m}
  72.       level = -caller(CALLER_OFFSET).size
  73.       while THIS_FILE =~ bt[level]
  74.         bt.delete_at(level)
  75.         level += 1
  76.       end
  77.       raise if klass            # if exception class is specified, it
  78.                                 # would be expected outside.
  79.       raise Error, e.message, e.backtrace
  80.     ensure
  81.       if y and y.alive?
  82.         y.kill
  83.         y.join # make sure y is dead.
  84.       end
  85.     end
  86.   end
  87.  
  88.   module_function :timeout
  89.  
  90. end
  91.  
  92. ##
  93. # Identical to:
  94. #
  95. #   Timeout::timeout(n, e, &block).
  96. #
  97. # Defined for backwards compatibility with earlier versions of timeout.rb, see
  98. # Timeout#timeout.
  99.  
  100. def timeout(n, e = nil, &block) # :nodoc:
  101.   Timeout::timeout(n, e, &block)
  102. end
  103.  
  104. ##
  105. # Another name for Timeout::Error, defined for backwards compatibility with
  106. # earlier versions of timeout.rb.
  107.  
  108. TimeoutError = Timeout::Error # :nodoc:
  109.  
  110. if __FILE__ == $0
  111.   p timeout(5) {
  112.     45
  113.   }
  114.   p timeout(5, TimeoutError) {
  115.     45
  116.   }
  117.   p timeout(nil) {
  118.     54
  119.   }
  120.   p timeout(0) {
  121.     54
  122.   }
  123.   p timeout(5) {
  124.     loop {
  125.       p 10
  126.       sleep 1
  127.     }
  128.   }
  129. end
  130.  
  131.